package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.user.utils.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:  就诊人列表接口
 * 单表的增删改查
 * @author: 化一帆
 * @date: 2022/12/6 17:58
 * @version: v1.0
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;


    /**
     * @description:  添加就诊人【保存前端传来的就诊人的数据到数据库】
     * 接收页面传过来的数据，并赋值userid
     * @author 化一帆
     * @date 2022/12/6 18:03
     */
    @PostMapping("/auth/save")
    public R savePatient(@RequestBody Patient patient, HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);//前端传过来的patient对象中userid没有值
        patientService.save(patient);
        return R.ok();
    }


    /**
     * @description:  根据当前用户的UserID查询当前用户的所有的就诊人
     * @author 化一帆
     * @date 2022/12/6 19:02
     */
    @GetMapping("auth/findAll")
    public R findAll(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list =patientService.findListByUserId(userId);
        return R.ok().data("list",list);
    }

    /**
     * @description:  查询某一个就诊人的详情
     * @author 化一帆
     * @date 2022/12/6 19:17
     */
    @GetMapping("auth/get/{id}")
    public R getPatient(@PathVariable Long id){
        Patient patient =patientService.getPatient(id);
        return R.ok().data("patient",patient);
    }


    /**
     * @description: 根据id修改就诊人的信息
     * @author 化一帆
     * @date 2022/12/6 19:23
     */
    @PostMapping("auth/update")
    public R updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }


    /**
     * @description: 删除id就诊人
     * @author 化一帆
     * @date 2022/12/6 19:27
     */
    @DeleteMapping("auth/remove/{id}")
    public R removePatient(@PathVariable Long id){
        patientService.removeById(id);
        return R.ok();
    }

    /**
     * @description: 根据就诊人的id获取就诊人信息
     * @author 化一帆
     * @date 2022/12/8 18:50
     */
    @GetMapping("inner/get/{id}")
    public Patient getPatientById(@PathVariable Long id){
        return patientService.getById(id);
    }
}
